package org.lgiavedoni.frank.view.service;

import org.lgiavedoni.android.franq.FrankMain;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;
import org.lgiavedoni.android.franq.service.config.ConfigService;
import org.lgiavedoni.android.franq.service.phone.ContextService;
import org.lgiavedoni.android.franq.util.implementation.ImplementationUtil;
import org.lgiavedoni.frank.view.R;
import org.lgiavedoni.frank.view.WordWidget;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.widget.RemoteViews;

public class FrankService extends Service {
	
	@Override
    //public void onStart(Intent intent, int startId) {
	public void onCreate() {
        

        // Build the widget update for today
        RemoteViews updateViews = buildUpdate(this);
        
        
        // Push update for this widget to the home screen
        ComponentName thisWidget = new ComponentName(this, WordWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);
        
        
        if(ContextService.inst.getApplicationContext()==null){
	        ContextService.inst.setApplicationContext(this);
	        ImplementationUtil.addImplementation("communication", org.lgiavedoni.frank.view.communication.CommunicationPhone.class);
	        //CommunicationPhone.inst.setView(R.layout.widget_word, R.id.definition, WordWidget.class);
	        
	        //add config files
	        ConfigService.inst.addPropertiesSource(getResources().openRawResource(R.raw.default_config));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.communication));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.core));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.commands));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.cron_tasks));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.daos));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.event_listeners));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.http_server_pages));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.interceptors));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.location_strategies));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.phone_listeners));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.prediction_strategies));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.services));
	        ImplementationUtil.addImplementationSource(getResources().openRawResource(R.raw.sync_process));
	        //start
	        FrankMain.start();  
        }
        
        //try to stay in this method or android can close the app
      /*  synchronized(flag){
        	try {
				flag.wait();
			} catch (InterruptedException e) {
				Logger.inst.error(e);
			}
        }*/
       
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    /**
     * Build a widget update to show the current Wiktionary
     * "Word of the day." Will block until the online API returns.
     */
    public RemoteViews buildUpdate(Context context) {
      RemoteViews views = null;
        

        	// Build an update that holds the updated widget contents
            views = new RemoteViews(context.getPackageName(), R.layout.widget_word);
            
           // String wordTitle = matcher.group(1);
            
            views.setTextViewText(R.id.definition, "Loading...");
            
            // When user clicks on widget, launch to Wiktionary definition page
          
            Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("wiktionary://lookup/blabla"));
            PendingIntent pendingIntent = PendingIntent.getActivity(context,
                    0 /* no requestCode */, defineIntent, 0 /* no flags */);
            views.setOnClickPendingIntent(R.id.widget, pendingIntent);
     
        return views;
    }
    
    @Override
	public void onLowMemory() {
    	CommunicationService.inst.sendDefaultTextMessage("onLowMemory");
    	super.onLowMemory();
	}

	@Override
	public void onDestroy() {
		CommunicationService.inst.sendDefaultTextMessage("onDestroy");
		super.onDestroy();
		onCreate();
	}
	
	
}
